package com.googlecode.wannabet.controller.ui;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.googlecode.wannabet.domain.Bet;
import com.googlecode.wannabet.domain.BetType;

public class CreateBetController extends Controller {

	@Override
	protected Navigation run() throws Exception {
		String submit = asString("submit");
		if(!"true".equals(submit)) {
			return forward("/jsps/createBet.jsp");
		}
		
		Long matchID = asLong("matchID");
		Long eventID = asLong("eventID");
		Long subEventID = asLong("subEventID");
		String eventDetails = asString("eventDetails");
		Double amount = asDouble("amount");
		
		UserService userService = UserServiceFactory.getUserService();
		
		String userID = userService.getCurrentUser().getUserId(); 
		Entity bet = Bet.getEntity(userID, BetType.MATCH_BET, matchID, eventID, subEventID, eventDetails, amount);
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.put(bet);
		
		return redirect("/ui/");
	}

}
